package person.drf.shop.product.jucTest;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Ticket{
    private int number = 50;

    private Lock lock = new ReentrantLock();

    public void saleTocket(){
        lock.lock();
        try {
            if(number>0){
                System.out.println(Thread.currentThread().getName()+"--卖出第"+number--+"张票");
            }
        }catch (Exception e){
           e.printStackTrace();
        }finally {
            lock.unlock();
        }
        
    }
}

public class TicketDemo {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();

        new Thread(()->{ for (int i = 0; i < 50; i++) { ticket.saleTocket();}
        },"A").start();
        new Thread(()->{ for (int i = 0; i < 50; i++) { ticket.saleTocket();}
        },"B").start();
        new Thread(()->{ for (int i = 0; i < 50; i++) { ticket.saleTocket();}
        },"C").start();


        /**
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 50; i++) {
                    ticket.saleTocket();
                }
            }
        },"A").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 50; i++) {
                    ticket.saleTocket();
                }
            }
        },"B").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 50; i++) {
                    ticket.saleTocket();
                }
            }
        },"C").start();

         **/
    }
}
